<?php
class OrderController extends AppController {
	var $name = 'Order';
	var $components = array('Menu','Init');
	var $uses = array();

	function beforeFilter() {
		$this->Init->startup($this);
		$this->Init->init();
		$this->login = $this->Init->init();
		$this->Init->getKeyword();
		if(empty($this->login['User']['id'])) {
			$this->redirect(array('controller' => 'login','action' => 'index','order'));
		}
		$_session = $this -> Session -> read();
		$this->loadModel('Hitcounter');
		$row = $this->Hitcounter->get(session_id());
		if ($row == 0) {
			$data = array(
				"session_id" => session_id(),
				"ref_url" => env("REQUEST_URI"),
				"ip_address" => env("REMOTE_ADDR"),
				"activity" => date("Y-m-d H:i:s")
			);
			$this->Hitcounter->save($data);
		}
		$this->set('activeNav', 'Home');
                 $this->set('vipDiscount', $this->Session->read('discount_for_vip'));
	}

	function index() {
		$this->layout = 'inside';
		$this->loadModel('ProductType');
		$this->loadModel('Product');
		$this->loadModel('Order');
		if (!$this->Session->read("lang")) {
			$lang = "vie";
		} else {
			$lang = $this->Session->read("lang");
		}

		Configure::write("Config.language", $lang);
		$this->set('lang',$lang);
		$orders = $this->Session->read('User.order');
		if (!empty($orders)) {
			$this->set('basket_kind',1);
			$this->set('orders', $orders);
		}
		else {
			$menus = $this->ProductType->find('list', array(
					'conditions' => array('ProductType.status' => 1),
					'fields' => array('ProductType.id','ProductType.name_'.$lang)
			));
			$this->set('lang',$lang);
				$products = $this->Product->find('list', array(
					'conditions' => array('Product.status' => 1,'Product.type' => 1),
					'fields' => array('Product.id','Product.name_'.$lang)
			));
			$this->set('products',$products);
			$this->set('product_types',$menus);
			$this->set('basket_kind',0);
		}

		$this->set('captcha_image_url', Router::url(array('controller'=>'captcha', 'action'=>'securimage'))); //url for the captcha image

                $captcha_error_msg = __('The code you entered does not match', true);

		if (isset($this->data)) {
			$this->data['Order']['status'] = 0;
			$this->data['Order']['date'] = date('Y-m-d H:i:s');
			if ($this->Order->save($this->data)) {
				$_order_id = $this->Order->getLastInsertId();
				$this->loadModel('OrderDetail');
				if (empty($orders)) {
					$orders[0]['id'] = $this->data['Order']['product'];
					$orders[0]['quantity'] = $this->data['Order']['quantity'];
				}
				foreach($orders as $item) {
					$this->OrderDetail->create();
					$_order_detail['product'] = $item['id'];
					$_order_detail['cost'] = $item['price'];
					$_order_detail['quantity'] = $item['quantity'];
					$_order_detail['order'] = $_order_id;
					
					if($this->OrderDetail->save($_order_detail)) {
						continue;
					} 
				}
				$this->Session->delete('User.order');
				$this->Session->setFlash(__('Succesfull order.', true));
				$this->redirect(array('controller' => 'member','action' => 'index'));
			}
		}
		$this->render('index');
	}
}
?>
